

var FloatC = new Object();

/**
 * 获取数字小数点后面length
 */
FloatC.digitLength = function(num) {
  var eSplit = num.toString().split(/[eE]/);
  var len = (eSplit[0].split('.')[1] || '').length;
  return len > 0 ? len : 0;
};

/**
 * 把小数转成整数
 */
FloatC.float2Fixed = function(num) {
  if (num.toString().indexOf('e') === -1) {
    return Number(num.toString().replace('.', ''));
  }
  var dLen = FloatC.digitLength(num);
  return dLen > 0 ? num * Math.pow(10, dLen) : num;
};


/**
 * 乘法
 */
FloatC.times = function(num1, num2) {
  if (!FloatC.isVal(num1, num2)) {
    return;
  }
  num1 = Number(num1);
  num2 = Number(num2);
  var thisChanged = FloatC.float2Fixed(num1);
  var numChanged = FloatC.float2Fixed(num2);
  var baseNum = FloatC.digitLength(num1) + FloatC.digitLength(num2);
  var leftValue = thisChanged * numChanged;
  return leftValue / Math.pow(10, baseNum);
};


/**
 * 加法
 */
FloatC.plus = function(num1, num2) {
  if (!FloatC.isVal(num1, num2)) {
    return;
  }
  num1 = Number(num1);
  num2 = Number(num2);
  var baseNum = Math.pow(10, Math.max(FloatC.digitLength(num1), FloatC.digitLength(num2)));
  return (FloatC.times(num1, baseNum) + FloatC.times(num2, baseNum)) / baseNum;
};

/**
 * 减法
 */
FloatC.minus = function(num1, num2) {
  if (!FloatC.isVal(num1, num2)) {
    return;
  }
  num1 = Number(num1);
  num2 = Number(num2);
  var baseNum = Math.pow(10, Math.max(FloatC.digitLength(num1), FloatC.digitLength(num2)));
  return (FloatC.times(num1, baseNum) - FloatC.times(num2, baseNum)) / baseNum;
};


/**
 * 除法
 */
FloatC.divide = function(num1, num2) {
  if (!FloatC.isVal(num1, num2)) {
    return;
  }
  num1 = Number(num1);
  num2 = Number(num2);
  var num1Changed = FloatC.float2Fixed(num1);
  var num2Changed = FloatC.float2Fixed(num2);
  return FloatC.times((num1Changed / num2Changed), Math.pow(10, FloatC.digitLength(num2) - FloatC.digitLength(num1)));
}

/**
 * 四舍五入
 */
FloatC.round = function(num, ratio) {
  if (!FloatC.isVal(num, ratio)) {
    return;
  }
  num = Number(num);
  ratio = Number(ratio);
  var base = Math.pow(10, ratio);
  return FloatC.divide(Math.round(FloatC.times(num, base)), base);
}

/**
 * 判断是否是数字类型
 */
FloatC.isVal = function(num1, num2) {
  var isValueB = true;
  num1 = num1 ? Number(num1) : 0;
  num2 = num2 ? Number(num2) : 0;
  var re = /^(\-|\+)?\d+(\.\d+)?$/;; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
  if (!re.test(num1) || !re.test(num2)) {
    isValueB = false
  }
  return isValueB;
}

// Precise calculation精确算法
function PC(num1, num2, type) {
  if (!FloatC.isVal(num1, num2)) {
    return;
  }
  num1 = Number(num1);
  num2 = Number(num2);
  var value;

  var mark = ['+', '-', '*', '/']
  if (!type) {
    alert('请填写第三个参数算法类型(+,-,*,/)');
    return;
  } else if (mark.indexOf(type) === -1) {
    alert('第三个参数算法类型(+,-,*,/)');
    return;
  }

  switch (type) {
    case "+":
      value = FloatC.plus(num1, num2);
      break;
    case "-":
      value = FloatC.minus(num1, num2);
      break;
    case "*":
      value = FloatC.times(num1, num2);
      break;
    case "/":
      value = FloatC.divide(num1, num2);
      break;
  }

  return value;
}

export default FloatC;